package com.gaofans.vertx.boot.dbutils.mapper.rows;

import com.gaofans.vertx.boot.dbutils.mapper.BeanProcessor;
import com.gaofans.vertx.boot.dbutils.mapper.RowMapper;
import com.gaofans.vertx.boot.dbutils.mapper.domain.ResultMapper;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import org.springframework.util.Assert;

import java.util.List;

public class BeanRowMapper<T> implements RowMapper<T> {

    private final ResultMapper resultMapper;
    private Class<T> type;

    public BeanRowMapper(ResultMapper resultMapper) {
        this.resultMapper = resultMapper;
    }

    @Override
    public T single(RowSet<Row> rs) {
        Assert.notNull(this.type,"未指定类型");
        return BeanProcessor.toBean(rs,resultMapper,type);
    }

    @Override
    public List<T> list(RowSet<Row> rs) {
        Assert.notNull(this.type,"未指定类型");
        return BeanProcessor.toList(rs,resultMapper,type);
    }

    @Override
    public void setType(Class<T> type) {
        this.type = type;
    }
}
